//
//  RSSParser.m
//  VNExpressRSS
//
//  Created by Vinh Huynh on 8/22/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "RSSParser.h"
#import "News.h"

@implementation RSSParser

@synthesize currentElement;
@synthesize currentElementValue;
@synthesize respondData;
@synthesize rssNews;
@synthesize news;
@synthesize appDelegate;
@synthesize isNews;

-(void) getNewsFromURL:(NSString *)url withDelegate:(id)aDelegate{
	NSURL *parserUrl = [[NSURL alloc] initWithString:url];
	NSURLRequest *request = [[NSURLRequest alloc] initWithURL:parserUrl];
	self.appDelegate = aDelegate; 
	[NSURLConnection connectionWithRequest:request delegate:self];
	NSLog(@" url : %@", url);
}

-(void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
	self.respondData = [[NSMutableData alloc] initWithLength:0];
}

-(void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
	[self.respondData appendData:data];
	//NSLog(@" Data : %@", self.respondData);
}

-(void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
	NSLog(@"Connect Fail");
}

-(void) connectionDidFinishLoading:(NSURLConnection *)connection{
	self.currentElement = [[[NSMutableString alloc] init] autorelease];
	self.currentElementValue = [[[NSMutableString alloc] init] autorelease];
	self.rssNews = [[[NSMutableArray alloc] init] autorelease];
	self.isNews = NO;
	NSXMLParser *parser = [[NSXMLParser alloc] initWithData:self.respondData];
	[parser setDelegate:self];
	[parser parse];
	[parser release];
	parser = nil;
}

-(void) parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI 
 qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
	if ([elementName isEqualToString:@"item"]) {
		self.isNews = YES;
		self.news = [[News alloc] init];
	}
	//NSLog(@"start");
	[self.currentElementValue setString:@""];
}

-(void) parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
	if(self.isNews == YES){
		if ([elementName isEqualToString:@"title"]) {
			NSString *titleString = [NSString stringWithFormat:@"%@", self.currentElementValue] ;
			self.news.title = titleString;
		//	NSLog(@"Title %@",self.news.title);
		}
		if ([elementName isEqualToString:@"link"]) {
		 	NSString *linkString = [NSString stringWithFormat:@"%@", self.currentElementValue] ;
			self.news.link = linkString;
		//	NSLog(@"Link: %@",self.news.link );
		}
		if ([elementName isEqualToString:@"pubDate"]) {
			NSString *pubDateString = [NSString stringWithFormat:@"%@",self.currentElementValue] ;
			self.news.pubDate = pubDateString;
		//	NSLog(@"pubDate: %@",self.news.pubDate);
		}
		if ([elementName isEqualToString:@"description"]) {
			NSString *descString = [NSString stringWithFormat:@"%@",self.currentElementValue];			
			NSArray *imgTempArray1 = [descString componentsSeparatedByString:@"src=\""];
			if ([imgTempArray1 count]>1) {
				NSArray *imgTempArray2 = [[imgTempArray1 objectAtIndex:1] componentsSeparatedByString:@"\">"];
				self.news.thumbnailLink = [imgTempArray2 objectAtIndex:0];
				NSData *thumbnailData =	[NSData dataWithContentsOfURL:[NSURL URLWithString:self.news.thumbnailLink]];
				self.news.thumbnail = [UIImage imageWithData:thumbnailData];
				//	NSLog(@"thumbnaiLink: %@", self.news.thumbnailLink );
				if ([imgTempArray2 count]>1) {
					NSArray *descTempArray1 = [[imgTempArray1 objectAtIndex:1] componentsSeparatedByString:@"a>"];
					if ([imgTempArray2 count] > 2) {
						self.news.description = [[NSArray arrayWithArray:[[descTempArray1 objectAtIndex:1]componentsSeparatedByString:@"<BR"]] objectAtIndex:0];
					//	NSLog(@"desc : %@",self.news.description);
					}
					else{
						self.news.description = [[NSArray arrayWithArray:[[descTempArray1 objectAtIndex:1]componentsSeparatedByString:@"]"]] objectAtIndex:0];
					//	NSLog(@"desc : %@",self.news.description);
					}
				}				
			}
			else {
				NSArray *descTempArray3 = [descString componentsSeparatedByString:@"<"]; 
				if ([descTempArray3 count]>1) {
					self.news.description = [descTempArray3 objectAtIndex:0];
				}
				else {
					self.news.description = [[NSArray arrayWithArray:[descString componentsSeparatedByString:@"]"]] objectAtIndex:0];
				}
				//NSLog(@"desc : %@",self.news.description);
				self.news.thumbnailLink = @"";
			}
		}
		if ([elementName isEqualToString:@"item"]) {
			self.isNews = NO;
			[rssNews addObject:self.news];
			[self.news release];
			news = nil;
			//NSLog(@"%@", self.rssNews);
		}
	}
	[self.currentElementValue setString:@""];
}

-(void) parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
	[self.currentElementValue appendString:string];
}

-(void) parserDidEndDocument:(NSXMLParser *)parser{
	[self.appDelegate didEndGetNews:rssNews];
}

-(void) dealloc {
	self.currentElement = nil;
	self.currentElementValue = nil;
	self.respondData = nil;
	self.rssNews = nil;
	[super dealloc];
}
@end
